package ch.fhnw.qualiscan.core.libraries;

import java.io.Serializable;
import java.util.ArrayList;

import ch.fhnw.qualiscan.AppObject;
import ch.fhnw.qualiscan.application.components.Navigation;
import ch.fhnw.qualiscan.application.components.SecondNavigation;
import ch.fhnw.qualiscan.core.LayoutComponent;
import eu.livotov.tpt.TPTApplication;

/**
 * This library takes care of loading the components. Components, are boxes with additional information for a certain page.
 * @author Benjamin Groehbiel, Kevin Mettenberger
 * @see ch.fhnw.qualiscan.core.LayoutComponent
 */
@SuppressWarnings("serial")
public class ComponentLib implements Serializable {
	
	AppObject dataObject = (AppObject) TPTApplication.getCurrentApplication().getMainWindow().getData();
	
	/**
	 * Displays components which are only shown when logged in.
	 */
	public void userLoggedIn() {
		// login status in the upper right corner
		SecondNavigation secondNav = new SecondNavigation();
		dataObject.getBaseLayout().headerBox2.removeAllComponents();
		dataObject.getBaseLayout().headerBox2.addComponent(secondNav);
		
		// navigation, to the right side of the logo
		Navigation navi = new Navigation();
		dataObject.getBaseLayout().headerBox1.removeAllComponents();
		dataObject.getBaseLayout().headerBox1.addComponent(navi);
	}
	
	/**
	 * Removes components which are shown when logged in. Displays components which are shown when logged out.
	 */
	public void userLoggedOut() {
		// status bar needs be instantiated again
		SecondNavigation secondNav = new SecondNavigation();
		dataObject.getBaseLayout().headerBox2.removeAllComponents();
		dataObject.getBaseLayout().headerBox2.addComponent(secondNav);
		
		// navigation
		dataObject.getBaseLayout().headerBox1.removeAllComponents();
	}
	
	/**
	 * This function updates the right sidebar, whenever a new page is loaded. The components are generated in the concrete page.
	 * @param components LayoutComponents wished to be loaded.
	 */
	public void updateRightSidebar(ArrayList<LayoutComponent> components) {
		dataObject.getBaseLayout().rightSidebar.removeAllComponents();
		
		if(components.size() == 0) {
			dataObject.getBaseLayout().rightSidebar.setVisible(false);
		} else {
			if(dataObject.getBaseLayout().rightSidebar.isVisible() == false) {
				dataObject.getBaseLayout().rightSidebar.setVisible(true);
			}
			
			for(Object o : components) {
				dataObject.getBaseLayout().rightSidebar.addComponent((LayoutComponent) o);
			}
		}
	}
	
}
